<?php

namespace app\api\lists\game;

use app\api\lists\BaseApiDataLists;
use app\common\enum\YesNoEnum;
use app\common\lists\ListsSearchInterface;
use app\common\model\game\Software;

/**
 * 软件列表
 * Class SoftwareLists
 * @package app\api\lists\game
 */
class SoftwareLists extends BaseApiDataLists implements ListsSearchInterface
{
    /**
     * @notes 搜索条件
     * @return \string[][]
     * @author 段誉
     * @date 2022/9/16 18:54
     */
    public function setSearch(): array
    {
        return [
            //'=' => ['cid']
        ];
    }


    /**
     * @notes 自定查询条件
     * @return array
     * @author 段誉
     * @date 2022/10/25 16:53
     */
    public function queryWhere()
    {
        $where = [
            ['is_show', '=', YesNoEnum::YES],
        ];

        return $where;
    }


    /**
     * @notes 获取软件列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 段誉
     * @date 2024/8/9 16:04
     */
    public function lists(): array
    {
        $orderRaw = 'is_recommend desc, sort desc';
        $name = $this->params['name'] ?? '';
        $where = $this->queryWhere();
        if ($name != '') {
            $where[] = ['name', 'LIKE', "%$name%"];
        }

        $field = 'id, name, short_desc, image, download_url, code, sort, intro, is_show, is_recommend, create_time, update_time';
        $result = Software::field($field)
            ->where($where)
            ->where($this->searchWhere)
            ->orderRaw($orderRaw)
            ->limit($this->limitOffset, $this->limitLength)
            ->select()
            ->toArray();

        return $result;
    }


    /**
     * @notes 获取软件数量
     * @return int
     * @author 段誉
     * @date 2022/9/16 18:55
     */
    public function count(): int
    {
        return 0;
    }
}